// 默认静态菜单树
var treeData = [ [ {
	"id" : 1,
	"text" : "业务模块",
	"state" : "open",
	"iconCls" : "icon-add",
	"attributes" : {
		"sn" : "business"
	}
} ],

[{
	"id" : 1,
	"text" : "系统模块",
	"state" : "open",
	"iconCls" : "icon-add",
	"attributes" : {
		"sn" : "system"
	}
}],

[{
	"id" : 1,
	"text" : "其他模块",
	"state" : "open",
	"iconCls" : "icon-add",
	"attributes" : {
		"sn" : "chart"
	}
}]
];

$(function() {
	
	//初始化选项卡控件
	$("#main_tabs").tabs({
		fit : true,
	});
	
	
	//初始化分类板控件
	$("#main_accordion").accordion({
		fit : true,
	});
	
	//初始化树
	$(".myTree").each(function(index,ul) {
		$(ul).tree({
			data:treeData[index],
			onClick:createTab
		});
		//加载子菜单
		//1.发送ajax请求获取子菜单数据
		var url = '/wms/systemMenu_loadMenus.action';
		$.post(url, {"qo.parentSn":$(ul).data("sn")}, function(data){
			var treeData = [];
			for (var i = 0; i < data.length; i++) {
				var tree = {};
				tree.id = data[i].id;
				tree.text = data[i].name;
				tree["attributes.url"] = data[i].url;
				tree.iconCls = "icon-remove";
				treeData[i] = tree;
			}
			//2.追加子菜单树
			$(ul).tree('append', {
				parent:$(ul).tree('find',1).target,
				data:treeData
				
			});
		}, "json");
	});
	
});

function createTab(node){
	var url = node["attributes.url"];
	//判断节点上是否有url属性
	if (url) {
		var tab = $("#main_tabs").tabs('getTab', node.text);
		//如果此节点的选项卡已经打开, 则切换到此面板并刷新
		if (tab) {
			$("#main_tabs").tabs('select',node.text);
			return;
		}
		$("#main_tabs").tabs('add',{
			title:node.text,
			closable:true,
			content:'<iframe src="/wms/'+url+'.action" width=100% height=100% frameborder=0></iframe>'
		});
	}
}

